 package com.zt.ztrob.controller;

import com.zt.ztrob.bean.Wechat;
import com.zt.ztrob.bean.WechatContact;
import com.zt.ztrob.common.GeneralResult;
import com.zt.ztrob.common.ResultCodeEnum;
import com.zt.ztrob.common.UserContext;
import com.zt.ztrob.service.WechatBotService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class WechatBotController {

    @Autowired
    private WechatBotService wechatBotService;

    /**
     * 获取微信二维码url
     *
     * @return url
     */
    @ResponseBody
    @RequestMapping("/iamge")
    public GeneralResult<String> getIamgeUrl() {
        GeneralResult<String> result = new GeneralResult<>();
        String url = wechatBotService.getQrcodeUrl();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getDesc());
        result.setData(url);
        return result;
    }
    
    /*@ResponseBody
    @RequestMapping("/image")
    public ModelAndView getCodeUrl() {
        GeneralResult<String> result = new GeneralResult<>();
        String url = wechatBotService.getQrcodeUrl();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getDesc());
        result.setData(url);
        return new ModelAndView("redirect:"+url);
    }*/

    @ResponseBody
    @RequestMapping("login")
    public String getUrl() {
        Map<String, Object> map=null;
        boolean islogin=false;
       while(!islogin){
        map = wechatBotService.login();
        islogin=map.get("code").equals("200")?true:false;
       }
        if(null != map) {
            while (map.get("code").equals("201")) {
                map = wechatBotService.login();
            }
            if (map.get("code").equals("200")) {
                /*result.setCode(ResultCodeEnum.SUCCESS.getCode());
                result.setMsg(ResultCodeEnum.SUCCESS.getDesc());
                result.setData(map);*/
                
                return "{\"errorCode\":\"200\",\"errorMsg\":\"success\"}";
            }
            if(map.get("code").equals("408")) {
                /*result.setCode(ResultCodeEnum.OVERTIME.getCode());
                result.setMsg(ResultCodeEnum.OVERTIME.getDesc());
                result.setData(map);*/
               return "{\"errorCode\":\"408\",\"errorMsg\":\"failed\"}";
            } else {
                /*result.setCode(ResultCodeEnum.FAILURE.getCode());
                result.setMsg(ResultCodeEnum.FAILURE.getDesc());
                result.setData(map);*/
               return "{\"errorCode\":\"999\",\"errorMsg\":\"failed\"}";
            }
        }
        /*result.setCode(ResultCodeEnum.FAILURE.getCode());
        result.setMsg(ResultCodeEnum.FAILURE.getDesc());*/
        return "{\"errorCode\":\"999\",\"errorMsg\":\"failed\"}";
       
    }

    @ResponseBody
    @RequestMapping("start")
    public GeneralResult<Wechat> start() {
    	wechatBotService.start();
    	return null;
    }
    
    @ResponseBody
    @RequestMapping("contact")
    public GeneralResult<WechatContact> webwxgetcontact() {
    	GeneralResult<WechatContact> result = new GeneralResult<>();
    	Wechat wechat = UserContext.getWechat();
    	if(wechat == null) {
    		result.setCode(ResultCodeEnum.FAILURE.getCode());
            result.setMsg(ResultCodeEnum.FAILURE.getDesc());
            return result;
    	}
        WechatContact wechatContact = wechatBotService.getContact();
        if (null != wechatContact) {
            result.setCode(ResultCodeEnum.SUCCESS.getCode());
            result.setMsg(ResultCodeEnum.SUCCESS.getDesc());
            result.setData(wechatContact);
        } else {
            result.setCode(ResultCodeEnum.FAILURE.getCode());
            result.setMsg(ResultCodeEnum.FAILURE.getDesc());
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("logout")
    public GeneralResult<String> logout() {
        UserContext.invalidate();
        GeneralResult<String> result = new GeneralResult<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getDesc());
        return result;
    }
    @ResponseBody
    @RequestMapping("zdsend")
    public String zdSendMsg(HttpServletRequest request) throws UnsupportedEncodingException{  
    	/*Wechat wechat=UserContext.getWechat();
        String name = request.getParameter("name");  
        String cont = new String(request.getParameter("cont").getBytes("iso8859-1"),"UTF-8"); 
        wechatBotService.zdsendMsg(wechat, cont, name);
        return null;*/
    	String robid=request.getParameter("robId");
    	String name = request.getParameter("name");  
    	// new String(request.getParameter("cont").getBytes("G"),"UTF-8")   	
        String cont =new String(request.getParameter("cont").getBytes("UTF-8"),"UTF-8"); 
        System.out.println(cont);
        String result=wechatBotService.zdsendMsg(robid, cont, name);
    	return result;

    } 
    
    @ResponseBody
    @RequestMapping("getgroup")
    public void getgroup(HttpServletRequest request) throws UnsupportedEncodingException{  
    	String groupId = request.getParameter("groupId"); 
    	wechatBotService.getGroup(groupId);
    } 
}
